import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import(/* webpackChunkName: "goods" */ '../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
    beforeEnter: (to, from, next) => {
      // ...
      let token = localStorage.getItem('x-auth-token')
      if(token){
        next();
      }else{
        store.dispatch("showToast/asyncChangeIsShowToast",{
          isshow:true,
          msg:"请登录账号",
          type:'danger'
        })
  
      }
    },
    children:[
     {
      path:"/user/Cart",
      name:'cart',
      component: () => import(/* webpackChunkName: "user/Cart" */ '../components/user/Cart.vue')
     }
    ]
    
  },
  {
    path: '/order',
    name: 'order',
    component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import(/* webpackChunkName: "about" */ '../views/Free.vue')
  },
  
  {
    path: '/detaills',
    name: 'detaills',
    component: () => import(/* webpackChunkName: "detaills" */ '../views/Detaills.vue')
  },{
    path:'*',// 通配符
    name:'error',
    component: () => import(/* webpackChunkName: "error" */ '../components/Error.vue')
  }
 
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// router.beforeEach((to,from,next)=>{
//   if(from.path=='/'){
//     let getLocal = localStorage.getItem('x-auth-token');
//     if(getLocal){
//       store.commit('logiinStatus/loginStatus', false);
//     }else{
//       store.commit('logiinStatus/loginStatus', true);
//     }
//   }
//   next();
// })


// router.beforeEach((to, from, next) => {
//   if(to.path=='/user'){
//     let token = localStorage.getItem('x-auth-token')
//     if(token){
//       next();
//     }else{
//       store.dispatch("showToast/asyncChangeIsShowToast",{
//         isshow:true,
//         msg:"请登录账号",
//         type:'danger'
//       })

//     }
//     return
//   }
//   next()
// })




export default router
